const install = (Vue, vm) => {
	Vue.prototype.$u.http.setConfig({
		loadingText: '努力加载中~',
		loadingTime: 800,
		loadingMask: false,
		originalData: true,
		baseUrl: ''
	})
	// 请求拦截，配置Token等参数
	Vue.prototype.$u.http.interceptor.request = (config) => {
		// console.log('config=', config)
		// console.log('config.url=' + config.url)

		//接口加随机数
		config.data.millisecond = new Date().getTime()

		// const prefix_api = '/api'
		// const prefix_api_ext = '/ext'
		if (vm.vuex_ip_current.ip == null) {
			throw new Error('ip端口配置不正确!')
		}
		const prefix_api = `http://${vm.vuex_ip_current.ip}:${vm.vuex_ip_current.server_port}`
		const prefix_api_ext = `http://${vm.vuex_ip_current.ip}:${vm.vuex_ip_current.device_port}`


		if (config.url.indexOf('ntka') !== -1) {
			config.url = prefix_api + config.url
		} else if (config.url.indexOf('server.html') !== -1) {
			config.url = prefix_api_ext + config.url
		}
		console.log('config.url=', config.url)
		//白名单接口开始
		//登录
		if (config.url.endsWith('/sys/login2') || config.url.indexOf('logo/getByType') !== -1) return config
		//白名单接口结束

		config.header['X-Access-Token'] = vm.vuex_token

		if (vm.vuex_token) {
			console.log("vuex_token exists")
		} else {
			console.log("vuex_token doesn't exist------------")
			//没有令牌code，重新登录
			uni.reLaunch({
				url: '/pages/login/index'
			})
			return
		}
		return config
	}
	// 响应拦截，判断状态码是否通过
	Vue.prototype.$u.http.interceptor.response = (res) => {
		console.log(res)
		if (res.statusCode === 401 || res.data.result == 2) {
			uni.showToast({
				title: '请重新登录',
				icon: 'none',
				success() {
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/index'
						})
					}, 1000)
				}
			})
		}
		return res.data
	}
}

export default {
	install
}